package com.starblink.android.basic.widget.dialog

import android.app.Activity
import android.content.DialogInterface
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.text.style.TextAppearanceSpan
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.starblink.android.basic.R
import com.starblink.android.basic.aroute.SubscriptionProvider
import com.starblink.android.basic.databinding.DialogSubscribeBinding
import com.starblink.android.basic.ext.toastSuccess
import com.starblink.android.basic.extension.navigation
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.basic.route.RoutePage
import com.starblink.basic.util.CheckUtil

/**
 * 订阅通用弹框
 */
class SubscribeDialog(
    val context: Activity,
    val email: String?,
    val registerEmail: String?,
    val title: String ,
    private val imageResource: Int,
    private val noEmailTipResource: Int,
    private val emailTipResource: Int,
    private val remindMeKey:String,
    private val subscribeNextSuccess: (() -> Unit)? = null
): BaseBottomSheetFragment(){
    lateinit var mBinding: DialogSubscribeBinding
    var useEmail :String ? = null
    override fun getBindingRootView(): View {
        mBinding = DialogSubscribeBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initView() {
        mBinding.tvSubscribeTitle.text= title
        mBinding.ivSubscribeSuccess.setImageResource(imageResource)

        if(!CheckUtil.isEmpty(email)){
            useEmail=email
        }else if(!CheckUtil.isEmpty(registerEmail)){
            useEmail=registerEmail
        }

        if(!CheckUtil.isEmpty(useEmail)){
            //已设置邮箱
            mBinding.tvEmailTip.text = "Manage My Alerts"
            val emailStr = getString(emailTipResource,useEmail)
            val start = emailStr.indexOf(useEmail!!)
            val end = start + useEmail!!.length
            val spannable =  SpannableStringBuilder(emailStr)
            spannable.setSpan(
                ForegroundColorSpan(
                    context.resources.getColor(com.starblink.basic.style.R.color.gu_green_900)
                ),
                start,
                end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            spannable.setSpan(
                TextAppearanceSpan(
                    context, com.starblink.basic.style.R.style.Guang2_Text_Label_L
                ),
                start,
                end,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE
            )
            mBinding.tvSubscribeEmail.text = spannable
        }else {
            //未设置邮箱
            mBinding.tvSubscribeEmail.text = getString(noEmailTipResource)
            //没有注册邮箱
            mBinding.tvEmailTip.text = "Set notification email"
            UserDataCenter.putSubscribeEmial("")
        }

        mBinding.llRemindAgain.setOnClickListener {
            //记录下一次不再弹框
            val remindMe=UserDataCenter.getSubscribeRemindMe(remindMeKey)
            mBinding.ivRemindCheck.setImageResource(if(remindMe) R.mipmap.icon_brand_checked else R.mipmap.icon_brand_check)
            UserDataCenter.putSubscribeRemindMe(remindMeKey,!remindMe)
        }
        mBinding.tvEmailTip.setOnClickListener {
            //设置邮箱 修改邮箱 弹框
            var dialogHeight = mBinding.llBottomSheet.height
            (ARouter.getInstance().build(RoutePage.Widget.WIDGET_SUBSCRIPTION).navigation()
                    as? SubscriptionProvider)?.showSubsEmailInputDialog(context,useEmail,dialogHeight){
                //关闭
                dismiss()
            }
        }
        mBinding.clGotitBtn.setOnClickListener {
            //关闭
            dismiss()
        }
        mBinding.ivClose.setOnClickListener {
            //关闭
            dismiss()
        }
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        subscribeNextSuccess?.invoke()
    }
}